//
//  HomeBookViewController.m
//  CarManager
//
//  Created by 王森林 on 2019/1/21.
//  Copyright © 2019年 王森林. All rights reserved.
//

#import "HomeBookViewController.h"
#import "UserRes.h"
#import <AlipaySDK/AlipaySDK.h>
#import <WXApi.h>
#import <WXApiObject.h>
#import "ShopInfoOtherViewController.h"
@interface HomeBookViewController ()<WXApiDelegate>
{
    
    dispatch_source_t _timer;
    
}
@property (nonatomic ,strong) UIScrollView *scroll;
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel  *typeLabel;
@property (nonatomic, strong) UILabel  *typeLabel1;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *dateLabel;

@property (nonatomic, strong) UIView *centerView;
@property (nonatomic, strong) UITextField *nameField;
@property (nonatomic, strong) UITextField *mobileField;
@property (nonatomic, strong) UITextField *codeField;
@property (nonatomic, strong) UITextField *addressField;
@property  (nonatomic, strong) UIButton *codeBtn;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIImageView *arrowImageView;
@property (nonatomic, strong) NSDictionary *bookDict;
@property (nonatomic, assign) NSInteger bookExpan; //1为未展开，2为展开
@property (nonatomic, strong) UIView *expanView;
@property (nonatomic, strong) UIButton *submitBtn;
@end

@implementation HomeBookViewController

- (void)dealloc{
    if (_timer != nil) {
        dispatch_source_cancel(_timer);
        _timer = nil;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"预定";
    self.bookExpan = 1;
    [self createUI];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(paySuccess) name:NotificationCenterName_PaySuccess object:nil];
    
    // Do any additional setup after loading the view.
}

- (void)paySuccess
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.navigationController popViewControllerAnimated:YES];
    });
    
}

//倒计时
- (void)startTimer{
    __block int counts = 60;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0*NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(_timer, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            if (counts <= 0) {
                dispatch_source_cancel(self->_timer);
                self.codeBtn.enabled = YES;
                self.codeBtn.titleLabel.text = @"获取验证码";
                [self.codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
                self.codeBtn.titleLabel.font = FONT_SIZE(14);
                [self.codeBtn setTitleColor:RGB(50, 50, 50) forState:UIControlStateNormal];
            }else{
                self.codeBtn.enabled = NO;
                self.codeBtn.titleLabel.text = [NSString stringWithFormat:@"%d秒后重新获取",counts];
                [self.codeBtn setTitle:[NSString stringWithFormat:@"%d秒后重新获取",counts] forState:UIControlStateNormal];
                [self.codeBtn setTitleColor:RGB(136, 136, 136) forState:UIControlStateNormal];
                self.codeBtn.titleLabel.font = FONT_SIZE(12);
            }
            counts --;
        });
    });
    dispatch_resume(_timer);
}

- (void)handleSubmit
{
    
    if ([CommonMethod isBlankString:self.mobileField.text]) {
        
        [SVProgressHUD showErrorWithStatus:@"请输入手机号码"];
       
        return ;
    }
    
    if ([CommonMethod isBlankString:self.codeField.text]) {
        
        [SVProgressHUD showErrorWithStatus:@"请输入验证码"];
        
        return ;
    }
    NSDictionary *params = @{
                             @"FKEY":[CommonMethod createMD5Key:@"mobile"],
                             @"mobile":self.mobileField.text,
                             @"code":self.codeField.text
                             };
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD showWithStatus:nil];
    });
    
    [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_70 params:params md5Paraname:@"" success:^(id  _Nullable response) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
//        [self performSegueWithIdentifier:@"settingpassword" sender:nil];
        [UIActionSheet showInView:self.view withTitle:@"选择支付方式" cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@[@"支付宝支付",@"微信支付"] tapBlock:^(UIActionSheet * _Nonnull actionSheet, NSInteger buttonIndex) {
            NSLog(@"index%ld",buttonIndex);
            if (buttonIndex == 0) {
                [self getOrderInfo:@"1"];
            }
            if (buttonIndex == 1) {
                [self getOrderInfo:@"2"];
            }
            
            
        }];
       
        
    } fail:^(NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        });
   
    }];
    
    
}


- (void)getOrderInfo:(NSString *)payType
{
    
    
    UserRes *user = [CommonMethod getUser];
    if (!user) {
        [[NSNotificationCenter defaultCenter]postNotificationName:NotificationCenterName_AlertLoginVC object:nil];
        return;
    }
    
    NSDictionary *infoDict =  [self.resultDict objectForKey:@"info"];
    NSArray *ysAry = [self.resultDict objectForKey:@"ysList"];
    if (ysAry.count == 0) {
        [SVProgressHUD showErrorWithStatus:@"商品颜色出现问题"];
        
        return ;
    }
    
    
    if ([CommonMethod isBlankString:self.mobileField.text]) {
        
        [SVProgressHUD showErrorWithStatus:@"请输入手机号码"];
        
        return ;
    }
    
    if ([CommonMethod isBlankString:self.nameField.text]) {
        
        [SVProgressHUD showErrorWithStatus:@"请输入联系人"];
        
        return ;
    }
    
    if ([CommonMethod isBlankString:self.addressField.text]) {
        
        [SVProgressHUD showErrorWithStatus:@"请输入地址"];
        
        return ;
    }
    NSDictionary *ysDict = ysAry[0];
    NSDictionary *params = @{
                             @"FKEY":[CommonMethod createMD5Key:@"TOKEN"],
                             @"TOKEN":user.TOKEN,
                             @"ZSCXID":[infoDict objectForKey:@"SALECAR_ID"],
                             @"YS0000":[ysDict objectForKey:@"DICTIONARIES_ID"],
                             @"TCDZ00":self.addressField.text,
                             @"USERNAME":user.mobile,
                             @"LXR000":self.nameField.text,
                             @"LXDH00":self.mobileField.text,
                             @"GMSL00":@"1",
                             @"DDZT00":@"2",
                             @"GMFS00":@"1"
                             };
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD showWithStatus:nil];
    });
    
    [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_Save_28 params:params md5Paraname:@"" success:^(id  _Nullable response) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        //        [self performSegueWithIdentifier:@"settingpassword" sender:nil];
//        [UIActionSheet showInView:self.view withTitle:@"选择支付方式" cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@[@"支付宝支付"] tapBlock:^(UIActionSheet * _Nonnull actionSheet, NSInteger buttonIndex) {
//            NSLog(@"index%ld",buttonIndex);
//
//            [self getPayInfo];
//
//
//        }];
        NSDictionary *dict = response;
        if (payType.intValue == 1) {
               [self getPayInfo: [dict objectForKey:@"ID"]];
        }
        
        if (payType.intValue == 2) {
            [self getPayInfoWeixin: [dict objectForKey:@"ID"]];
        }
        
    } fail:^(NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        });
        
    }];
    
}
//微信支付
-  (void)getPayInfoWeixin:(NSString *)oderId
{
    
    UserRes *user = [CommonMethod getUser];
    if (!user) {
        [[NSNotificationCenter defaultCenter]postNotificationName:NotificationCenterName_AlertLoginVC object:nil];
        return;
    }
    NSDictionary *params = @{
                             @"FKEY":[CommonMethod createMD5Key:@"ORDERINFO_ID"],
                             @"TOKEN":user.TOKEN,
                             @"ORDERINFO_ID":oderId
                             
                             };
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD showWithStatus:nil];
    });
    
    [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_69 params:params md5Paraname:@"" success:^(id  _Nullable response) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        
        NSDictionary *dict = [response objectForKey:@"result"];
        [WXApi registerApp:dict[@"appid"]];
        PayReq *request = [[PayReq alloc] init];
        request.partnerId = dict[@"mch_id"];
        request.prepayId= dict[@"prepay_id"];
         request.package = @"Sign=WXPay";
        request.nonceStr= dict[@"nonce_str"];
        request.timeStamp= (uint32_t)[dict[@"timestamp"] longValue];
        request.sign= dict[@"sign"];
        [WXApi sendReq:request];
        
//        [[AlipaySDK defaultService]payOrder:dict[@"body"] fromScheme:@"CarUser" callback:^(NSDictionary *resultDic) {
//            NSLog(@"result = %@",resultDic);
//            NSString *result = resultDic[@"result"];
//            BOOL isok = NO;
//            if (result.length>0) {
//                id resultDic = [result mj_JSONObject];
//                if ([resultDic isKindOfClass:[NSDictionary class]]) {
//                    if ([resultDic[@"alipay_trade_app_pay_response"][@"msg"] isEqualToString:@"Success"]) {
//                        isok = YES;
//                    }
//                }
//            }
//            if ([resultDic[@"resultStatus"] integerValue] == 9000 && isok) {
//
//                dispatch_async(dispatch_get_main_queue(), ^{
//                    [self.navigationController popViewControllerAnimated:YES];
//                    [SVProgressHUD showInfoWithStatus:@"付款成功"];
//                    //                [SVProgressHUD dismissWithDelay:10];
//                });
//                //                [[NSNotificationCenter defaultCenter] postNotificationName:BX_NotificationCenterName_AlipayPayBack object:nil userInfo:@{@"success":@"1",@"message":[NSString stringWithFormat:@"%@",resultDic[@"memo"]]}];
//            }else{
//                //                 [SVProgressHUD showErrorWithStatus:@"付款失败"];
//
//                dispatch_async(dispatch_get_main_queue(), ^{
//                    [SVProgressHUD showInfoWithStatus:@"付款失败"];
//                    //                [SVProgressHUD dismissWithDelay:10];
//                });
//
//                //                [[NSNotificationCenter defaultCenter] postNotificationName:BX_NotificationCenterName_AlipayPayBack object:nil userInfo:@{@"success":@"0",@"message":[NSString stringWithFormat:@"%@",resultDic[@"memo"]]}];
//            }
//        }];
        

        
    } fail:^(NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        });
        
    }];
    
}

//支付宝支付
-  (void)getPayInfo:(NSString *)oderId
{
    
     UserRes *user = [CommonMethod getUser];
    if (!user) {
        [[NSNotificationCenter defaultCenter]postNotificationName:NotificationCenterName_AlertLoginVC object:nil];
        return;
    }
    NSDictionary *params = @{
                             @"FKEY":[CommonMethod createMD5Key:@"ORDERINFO_ID"],
                             @"TOKEN":user.TOKEN,
                             @"ORDERINFO_ID":oderId
                         
                             };
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD showWithStatus:nil];
    });
    
    [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_65 params:params md5Paraname:@"" success:^(id  _Nullable response) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });

        NSDictionary *dict = response;
        
        [[AlipaySDK defaultService]payOrder:dict[@"body"] fromScheme:@"CarUser" callback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
            NSString *result = resultDic[@"result"];
            BOOL isok = NO;
            if (result.length>0) {
                id resultDic = [result mj_JSONObject];
                if ([resultDic isKindOfClass:[NSDictionary class]]) {
                    if ([resultDic[@"alipay_trade_app_pay_response"][@"msg"] isEqualToString:@"Success"]) {
                        isok = YES;
                    }
                }
            }
            if ([resultDic[@"resultStatus"] integerValue] == 9000 && isok) {
               
                dispatch_async(dispatch_get_main_queue(), ^{
                     [self.navigationController popViewControllerAnimated:YES];
                    [SVProgressHUD showInfoWithStatus:@"付款成功"];
                    //                [SVProgressHUD dismissWithDelay:10];
                });
//                [[NSNotificationCenter defaultCenter] postNotificationName:BX_NotificationCenterName_AlipayPayBack object:nil userInfo:@{@"success":@"1",@"message":[NSString stringWithFormat:@"%@",resultDic[@"memo"]]}];
            }else{
//                 [SVProgressHUD showErrorWithStatus:@"付款失败"];
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    [SVProgressHUD showInfoWithStatus:@"付款失败"];
                    //                [SVProgressHUD dismissWithDelay:10];
                });
                
//                [[NSNotificationCenter defaultCenter] postNotificationName:BX_NotificationCenterName_AlipayPayBack object:nil userInfo:@{@"success":@"0",@"message":[NSString stringWithFormat:@"%@",resultDic[@"memo"]]}];
            }
        }];
        
//        [dict objectForKey:@"body"];
//        [self getPayInfo: [dict objectForKey:@"75f3fe547b4f4a3e9b56a73bccdc75d9"]];
        
    } fail:^(NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        });
        
    }];
    
}

- (void)updateBookView
{
    if (self.bookExpan == 2) {
        self.expanView = [[UIView alloc] init];
        self.expanView.backgroundColor = Common_Head_Color;
        [self.bottomView addSubview:self.expanView];
     
//        [self.expanView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.mas_equalTo(self.bottomView.mas_left).offset(0);
//            make.right.mas_equalTo(self.bottomView.mas_right).offset(0);
//            make.top.mas_equalTo(self.bottomView.mas_top).offset(45);
//        }];
     
        UILabel *contentLabel = [UILabel new];
        contentLabel.numberOfLines = 0;
       NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[[self.bookDict objectForKey:@"PZNR00"] dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        [attrStr addAttribute:NSBackgroundColorAttributeName value:[UIColor clearColor] range:NSMakeRange(0, attrStr.length)];
        contentLabel.attributedText = attrStr;
        contentLabel.font = FONT_SIZE(14);
        contentLabel.textColor = Common_TextColor;
        [self.expanView addSubview:contentLabel];
        [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.expanView.mas_left).offset(15);
            make.right.mas_equalTo(self.expanView.mas_right).offset(-15);
            make.top.mas_equalTo(self.expanView.mas_top).offset(15);
        }];
        
        
        CGSize maxSize = CGSizeMake(BOUNDWIDTH- 30, MAXFLOAT);
        CGRect contentRect = [attrStr boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
        
        self.expanView.frame = CGRectMake(0, 40, BOUNDWIDTH, contentRect.size.height);
        self.bottomView.frame = CGRectMake(0, 410, BOUNDWIDTH, contentRect.size.height +  40);
        self.submitBtn.frame = CGRectMake(15, 410+ contentRect.size.height +  40 +30, BOUNDWIDTH-30, 40);
        self.scroll.contentSize =  CGSizeMake(BOUNDWIDTH, 1300+ self.bottomView.origin.y);

//
        
    } else {
        [self.expanView removeFromSuperview];
        self.expanView= nil;
        self.bottomView.frame =  CGRectMake(0, 410, BOUNDWIDTH, 40);
        
        self.scroll.contentSize =  CGSizeMake(BOUNDWIDTH, self.bottomView.origin.y + 200);
         self.submitBtn.frame = CGRectMake(15, 480, BOUNDWIDTH-30, 40);

    }
    
}

//获取预定须知
- (void)handleBookTetxt
{
    
    if (self.bookExpan == 1) {
        self.bookExpan = 2;
        if (self.bookDict) {
            [self updateBookView];
            return;
        }
        NSDictionary *params = @{
                                 @"FKEY":[CommonMethod createMD5Key:@"PZLXDM"],
                                 @"PZLXDM":@"3"
                                 };
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showWithStatus:nil];
        });
        
        [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_31 params:params md5Paraname:@"" success:^(id  _Nullable response) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
            });
            self.bookDict = [response objectForKey:@"info"];
            [self updateBookView];
            //        [subscriber sendCompleted];
            
        } fail:^(NSError * _Nonnull error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD showErrorWithStatus:error.localizedDescription];
            });
            //        [subscriber sendCompleted];
        }];
    } else {
        self.bookExpan = 1;
        [self updateBookView];
      
        
    }
  
    
   
}

//获取验证码
- (void)handleGetCode
{
    
    if ([CommonMethod isBlankString:self.mobileField.text]) {
        [SVProgressHUD showErrorWithStatus:@"请输入手机号码"];
        return ;
    }
    
    NSDictionary *params = @{
                             @"FKEY":[CommonMethod createMD5Key:@"mobile"],
                             @"mobile":self.mobileField.text,
                             @"type":@"1"
                             };
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD showWithStatus:nil];
    });
    
    [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_CreateIdentifyCode params:params md5Paraname:@"" success:^(id  _Nullable response) {
        [self startTimer];
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
     
        
    } fail:^(NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        });
        //        [subscriber sendCompleted];
    }];
}

- (void)handleCompanyTap:(UIGestureRecognizer *)gesture
{
    ShopInfoOtherViewController *controller = [[ShopInfoOtherViewController alloc] init];
    NSDictionary *info = [self.resultDict objectForKey:@"info"];
    controller.TRADERS_ID = [info objectForKey:@"TRADERS_ID"];

    
    [self.navigationController pushViewController:controller animated:YES];
    
}


- (void)handleTap:(UIGestureRecognizer *)gesture
{
    
    [self handleBookTetxt];
}


- (void)createUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.scroll.frame = CGRectMake(0, 0, BOUNDWIDTH, BOUNDHEIGHT);
    self.scroll.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:self.scroll];
    //    [self.scroll mas_makeConstraints:^(MASConstraintMaker *make) {
    //         make.left.mas_equalTo(self.view.mas_left).offset(0);
    //        make.width.mas_equalTo(BOUNDWIDTH);
    //         make.top.mas_equalTo(self.view.mas_top).offset(0);
    //        make.height.mas_equalTo(BOUNDHEIGHT);
    //    }];
    //
    //    self.scroll.contentSize = CGSizeMake(BOUNDWIDTH, BOUNDHEIGHT);
    
    [self.scroll addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        //        (void)make.left.and.right;
        make.left.mas_equalTo(self.scroll.mas_left).offset(0);
        make.top.mas_equalTo(self.scroll.mas_top).offset(0);
        make.width.mas_equalTo(BOUNDWIDTH);
        make.height.mas_equalTo(150);
    }];
    
    [self.topView addSubview:self.iconImageView];
    [self.topView addSubview:self.titleLabel];
    [self.topView addSubview:self.typeLabel];
    [self.topView addSubview:self.typeLabel1];
    [self.topView addSubview:self.priceLabel];
    [self.topView addSubview:self.dateLabel];
    
    NSDictionary *infoDict = [self.resultDict objectForKey:@"info"];
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:APPEND_URL(BASE_URL, [infoDict objectForKey:@"ZST000"])]];
    //[infoDict objectForKey:@"CXMC00"]
    
    self.titleLabel.text = [infoDict objectForKey:@"CXMC00"];
    self.typeLabel.text = [infoDict objectForKey:@"GGBBMC"];
     self.typeLabel1.text = [infoDict objectForKey:@"GHFSMC"];
    //    self.priceLabel.text = @"¥28.18万";
    self.priceLabel.text = [NSString stringWithFormat:@"订金¥%@元",[infoDict objectForKey:@"ZDYFJE"]];
    
    self.dateLabel.text = [NSString stringWithFormat:@"%@",[infoDict objectForKey:@"SJRQ00"]];
    
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_top).offset(10);
        make.left.equalTo(self.topView.mas_left).offset(15);
        make.width.mas_equalTo(140);
        make.height.mas_equalTo(90);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_top).offset(15);
        make.left.equalTo(self.iconImageView.mas_right).offset(8);
        make.right.mas_equalTo(self.topView.mas_right).offset(-15);
        
    }];
    
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(10);
        make.left.equalTo(self.iconImageView.mas_right).offset(8);
        make.width.mas_equalTo(38);
        make.height.mas_equalTo(18);
    }];
    
    [self.typeLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(10);
        make.left.equalTo(self.typeLabel.mas_right).offset(10);
        make.width.mas_equalTo(38);
        make.height.mas_equalTo(18);
    }];
    
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.iconImageView.mas_bottom).offset(0);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(17);
        make.left.equalTo(self.iconImageView.mas_right).offset(8);
    }];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.iconImageView.mas_bottom).offset(0);
//        make.width.mas_equalTo(100);
        make.left.mas_equalTo(self.dateLabel.mas_right).offset(10);
        make.height.mas_equalTo(17);
        make.right.equalTo(self.topView.mas_right).offset(-16);
    }];
    
    UILabel *line = [[UILabel alloc] init];
    line.backgroundColor = Common_SepeColor;
    [self.topView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.iconImageView.mas_bottom).offset(10);
        make.width.mas_equalTo(BOUNDWIDTH);
        make.height.mas_equalTo(0.5);
        make.left.mas_equalTo(self.topView.mas_left);
    }];
    
    UIView *companyView = [[UIView alloc] init];
    UITapGestureRecognizer *companyTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleCompanyTap:)];
    [companyView addGestureRecognizer:companyTap];
    [self.topView addSubview:companyView];
    [companyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.iconImageView.mas_bottom).offset(11);
        make.width.mas_equalTo(BOUNDWIDTH);
        make.bottom.mas_equalTo(self.topView.mas_bottom).offset(0);
        make.left.mas_equalTo(self.topView.mas_left);
    }];
    
    UIImageView *imageView1 = [[UIImageView alloc] init];
    imageView1.image = IMG(@"cardetail_1");
    [companyView addSubview:imageView1];
    [imageView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(companyView.centerY);
        make.width.mas_equalTo(15);
        make.height.mas_equalTo(15);
        make.left.mas_equalTo(companyView.mas_left).offset(15);
    }];
    
    UIImageView *imageView2 = [[UIImageView alloc] init];
    imageView2.image = IMG(@"cardetail_2");
    [companyView addSubview:imageView2];
    [imageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(companyView.centerY);
        make.width.mas_equalTo(15);
        make.height.mas_equalTo(15);
        make.left.mas_equalTo(imageView1.mas_right).offset(10);
    }];
    
    
    UILabel  *companyLabel = [UILabel new];
    companyLabel.textColor = [UIColor whiteColor];
    companyLabel.font = FONT_SIZE(14);
    companyLabel.text = [infoDict objectForKey:@"JXSMC0"];
    [companyView addSubview:companyLabel];
    
    [companyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(companyView.centerY);
        make.right.mas_equalTo(companyView.mas_right).offset(-15);
        make.height.mas_equalTo(15);
        make.left.mas_equalTo(imageView2.mas_right).offset(10);
    }];
    
    [self createCenterView];
    [self createBottomView];
    
    self.submitBtn.frame = CGRectMake(15, 480, BOUNDWIDTH-30, 40);
    [self.scroll addSubview:self.submitBtn];
    
}


- (void)createBottomView
{
    [self.scroll addSubview:self.bottomView];
    self.bottomView.frame = CGRectMake(0, 410, BOUNDWIDTH, 40);
    
    UIView *bookView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, BOUNDWIDTH , 40)];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [bookView addGestureRecognizer:tap];
    [self.bottomView addSubview:bookView];

    UIImage *bookImage = IMG(@"book5");
    UIImageView *bookImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15,(40- IMGHEIGHT(bookImage))/2 , IMGWIDTH(bookImage), IMGHEIGHT(bookImage))];
    bookImageView.image = bookImage;
    [bookView addSubview:bookImageView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(30+IMGWIDTH(bookImage), 13, 100, 14)];
    titleLabel.text = @"发车须知";
    titleLabel.font = FONT_SIZE(15);
    titleLabel.textColor = Common_TextColor;
    [bookView addSubview:titleLabel];
    
    self.arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake( BOUNDWIDTH - 28,16, 13, 8)];
    self.arrowImageView.image = IMG(@"help_down");
    [bookView addSubview:self.arrowImageView];
    
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 39.5, BOUNDWIDTH, 0.5)];
    lineLabel.backgroundColor = Common_SepeColor;
    [bookView addSubview:lineLabel];
    
}

- (void)createCenterView
{
    
    [self.scroll addSubview:self.centerView];
    self.centerView.frame = CGRectMake(0, 150, BOUNDWIDTH, 260);
    
    //姓名
    UIView *nameView = [[UIView alloc] initWithFrame:CGRectMake(15, 15, BOUNDWIDTH - 30, 45)];
    nameView.backgroundColor = Common_BackgroundColor;
    nameView.layer.cornerRadius = 20;
    nameView.layer.masksToBounds = YES;
    [self.centerView addSubview:nameView];
    
    UIImage *nameImage = IMG(@"book1");
    UIImageView *nameImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,(45- IMGHEIGHT(nameImage))/2 , IMGWIDTH(nameImage), IMGHEIGHT(nameImage))];
    nameImageView.image = nameImage;
    [nameView addSubview:nameImageView];
    
    self.nameField = [[UITextField alloc] initWithFrame:CGRectMake(18+IMGWIDTH(nameImage), 7, BOUNDWIDTH - 60-IMGWIDTH(nameImage), 30)];
    self.nameField.font = FONT_SIZE(15);
    self.nameField.placeholder = @"请输入姓名";
    [nameView addSubview:self.nameField];
    
    //电话号码
    UIView *mobileView = [[UIView alloc] initWithFrame:CGRectMake(15, 75, BOUNDWIDTH - 30, 45)];
    mobileView.backgroundColor = Common_BackgroundColor;
    mobileView.layer.cornerRadius = 20;
    mobileView.layer.masksToBounds = YES;
    [self.centerView addSubview:mobileView];
    
    UIImage *mobileImage = IMG(@"book2");
    UIImageView *mobileImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15,(45- IMGHEIGHT(mobileImage))/2 , IMGWIDTH(mobileImage), IMGHEIGHT(mobileImage))];
    mobileImageView.image = mobileImage;
    [mobileView addSubview:mobileImageView];
    
    self.mobileField = [[UITextField alloc] initWithFrame:CGRectMake(30+IMGWIDTH(mobileImage), 7, BOUNDWIDTH - 60-IMGWIDTH(mobileImage), 30)];
    self.mobileField.font = FONT_SIZE(15);
    self.mobileField.placeholder = @"请输入手机号码";
    self.mobileField.keyboardType = UIKeyboardTypeNumberPad;
    [mobileView addSubview:self.mobileField];
    
    
    //验证码
    
    [self.centerView addSubview:self.codeBtn];
    self.codeBtn.frame =  CGRectMake(BOUNDWIDTH - 120, 140, 100, 35);
    
    
    
    UIView *codeView = [[UIView alloc] initWithFrame:CGRectMake(15, 135, BOUNDWIDTH - 145, 45)];
    codeView.backgroundColor = Common_BackgroundColor;
    codeView.layer.cornerRadius = 20;
    codeView.layer.masksToBounds = YES;
    [self.centerView addSubview:codeView];
    
    UIImage *codeImage = IMG(@"book3");
    UIImageView *codeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15,(45- IMGHEIGHT(codeImage))/2 , IMGWIDTH(codeImage), IMGHEIGHT(codeImage))];
    codeImageView.image = codeImage;
    [codeView addSubview:codeImageView];
    
    self.codeField = [[UITextField alloc] initWithFrame:CGRectMake(30+IMGWIDTH(codeImage), 7, BOUNDWIDTH - 60-IMGWIDTH(codeImage), 30)];
    self.codeField.font = FONT_SIZE(15);
    self.codeField.placeholder = @"请输入验证码";
    [codeView addSubview:self.codeField];
    
    
    
    //提车地点
    UIView *addressView = [[UIView alloc] initWithFrame:CGRectMake(15, 200, BOUNDWIDTH - 30, 45)];
    addressView.backgroundColor = Common_BackgroundColor;
    addressView.layer.cornerRadius = 20;
    addressView.layer.masksToBounds = YES;
    [self.centerView addSubview:addressView];
    
    UIImage *addressImage = IMG(@"book4");
    UIImageView *addressImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15,(45- IMGHEIGHT(mobileImage))/2 , IMGWIDTH(addressImage), IMGHEIGHT(addressImage))];
    addressImageView.image = addressImage;
    [addressView addSubview:addressImageView];
    
    self.addressField = [[UITextField alloc] initWithFrame:CGRectMake(25+IMGWIDTH(addressImage), 7, BOUNDWIDTH - 60-IMGWIDTH(addressImage), 30)];
    self.addressField.font = FONT_SIZE(15);
    self.addressField.placeholder = @"请输入提车地点";
    [addressView addSubview:self.addressField];
    
    
    
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 259.5, BOUNDWIDTH, 0.5)];
    lineLabel.backgroundColor = Common_SepeColor;
    [self.centerView addSubview:lineLabel];
    
    
}


- (UIScrollView *)scroll
{
    if (!_scroll) {
        _scroll = [[UIScrollView alloc] init];
    }
    return _scroll;
    
}


- (UIView *)topView
{
    if (!_topView) {
        _topView = [UIView new];
        _topView.backgroundColor = Common_HeadTitle_Color;
        
        
    }
    return _topView;
}

- (UIView *)centerView
{
    if (!_centerView) {
        _centerView = [UIView new];
        _centerView.backgroundColor = [UIColor whiteColor];
        
        
    }
    return _centerView;
}


- (UIButton *)submitBtn
{
    if (!_submitBtn) {
        _submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_submitBtn setBackgroundColor:RGB(254, 218, 60)];
        _submitBtn.layer.cornerRadius = 15;
        _submitBtn.layer.masksToBounds = YES;
        _submitBtn.titleLabel.font = FONT_SIZE(13);
        [_submitBtn setTitleColor:Common_TextColor forState:UIControlStateNormal];
        [_submitBtn addTarget:self action:@selector(handleSubmit) forControlEvents:UIControlEventTouchUpInside];
        [_submitBtn setTitle:@"提交" forState:UIControlStateNormal];
    }
    return _submitBtn;
    
}

- (UIButton *)codeBtn
{
    if (!_codeBtn) {
        _codeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_codeBtn setBackgroundColor:RGB(254, 218, 60)];
        _codeBtn.layer.cornerRadius = 15;
        _codeBtn.layer.masksToBounds = YES;
        _codeBtn.titleLabel.font = FONT_SIZE(14);
        [_codeBtn setTitleColor:RGB(50, 50, 50) forState:UIControlStateNormal];
        [_codeBtn addTarget:self action:@selector(handleGetCode) forControlEvents:UIControlEventTouchUpInside];
        [_codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    }
    return _codeBtn;
    
}

- (UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [UIView new];
//        _bottomView.backgroundColor = Common_HeadTitle_Color;
        
        
    }
    return _bottomView;
}


- (UIImageView *)iconImageView
{
    if (!_iconImageView) {
        _iconImageView = [UIImageView new];
        
    }
    return _iconImageView;
}


- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.font = FONT_SIZE(14);
        _titleLabel.numberOfLines =  2;
    }
    return _titleLabel;
    
}

- (UILabel *)typeLabel
{
    if (!_typeLabel) {
        _typeLabel = [UILabel new];
        _typeLabel.textColor = [UIColor whiteColor];
        _typeLabel.textAlignment = NSTextAlignmentCenter;
        _typeLabel.font = FONT_SIZE(12);
        _typeLabel.layer.borderWidth = 1;
        _typeLabel.layer.cornerRadius = 6;
        _typeLabel.layer.masksToBounds = YES;
        _typeLabel.layer.borderColor = [UIColor whiteColor].CGColor;
    }
    return _typeLabel;
    
}

- (UILabel *)typeLabel1
{
    if (!_typeLabel1) {
        _typeLabel1 = [UILabel new];
        _typeLabel1.textColor = [UIColor whiteColor];
        _typeLabel1.textAlignment = NSTextAlignmentCenter;
        _typeLabel1.font = FONT_SIZE(12);
        _typeLabel1.layer.borderWidth = 1;
        _typeLabel1.layer.cornerRadius = 6;
        _typeLabel1.layer.masksToBounds = YES;
        _typeLabel1.layer.borderColor = [UIColor whiteColor].CGColor;
    }
    return _typeLabel1;
    
}
- (UILabel *)priceLabel
{
    if (!_priceLabel) {
        _priceLabel = [UILabel new];
        _priceLabel.textColor = RGB(254, 218, 60);
        _priceLabel.textAlignment = NSTextAlignmentRight;
        _priceLabel.font = B_FONT_SIZE(14);
        
    }
    return _priceLabel;
    
}


- (UILabel *)dateLabel
{
    if (!_dateLabel) {
        _dateLabel = [UILabel new];
        _dateLabel.textColor = Common_TextLowColor;
        _dateLabel.textAlignment = NSTextAlignmentLeft;
        _dateLabel.font = FONT_SIZE(12);
    }
    return _dateLabel;
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
